import { isString } from "./typeValidator";

// 去除表单参数前后空格 返回新结果（避免影响用户输入）
export function trimParams(params: Record<string, unknown>) {
  const res = { ...params };
  for (let [k, v] of Object.entries(res)) {
    if (isString(v)) res[k] = v.trim();
  }
  return res;
}

export function simpleUniqueId(prefix = "id") {
  return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2)}`;
}

// 把树形结构数据转成hash
export function getDicurlMap(arr: Custom.TreeData | undefined, key: string, val: string) {
  function traverse(arr: Custom.TreeData) {
    arr.forEach((node) => {
      if (node.children) {
        traverse(node["children"]);
      }

      dict[node[key]!] = node[val]! as string;
    });
  }
  const dict: Record<string, string> = {};
  if (arr) traverse(arr);
  return dict;
}
